﻿using System;
using System.Collections.Generic;

namespace Com.OpenCode.Framework.Core
{
    public class Addins
    {
        private List<Addin> addins = new List<Addin>();

        public virtual int Count
        {
            get { return addins.Count; }
        }

        public virtual void Add(Addin addin)
        {
            if (addin != null)
            {
                addins.Add(addin);
            }
        }

        public virtual void Remove(Addin addin)
        {
            addins.Remove(addin);
        }

        public virtual void Remove(int index)
        {
            addins.RemoveAt(index);
        }

        public virtual Addin Get(int index)
        {
            return addins[index];
        }

        public virtual Addin Get(string name)
        {
            return addins.Find(p => p.Name == name);
        }

        public virtual Addin Get(Predicate<Addin> match)
        {
            return addins.Find(match);
        }

        public virtual void Sort()
        {
            addins.Sort(new AddinComparer());
        }

        public virtual void Genetic()
        {
            for (int i = 0; i < addins.Count; i++)
            {
                Addin mainAddin = addins[0];
                mainAddin.Genetic(addins[i]);
            }
            for (int i = 0; i < addins.Count; i++)
            {
                addins[i].Genetic(addins[0]);
            }
        }
    }
}